package com.egene.tw.activity.login;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import com.egene.tw.R;
import com.egene.tw.base.BaseActivity;
import com.egene.tw.model.User;
import com.egene.tw.ui.dialog.ChooseSexDialog;
import com.egene.tw.utils.CheckUtil;
import com.egene.tw.utils.MToast;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.parceler.Parcels;

import java.util.Calendar;
import java.util.GregorianCalendar;

@EActivity(R.layout.activity_register)
public class RegisterActivity extends BaseActivity {
    private static final int REQUEST_V_CODE = 0x10;
    //ToolBar Views
    @ViewById(R.id.actionBar)
    Toolbar mToolbarView;
    @ViewById(R.id.toolbar_title)
    TextView mToolBarTitle;

    //register message views
    @ViewById
    EditText etAccount;
    @ViewById
    EditText etPw;
    @ViewById
    EditText etConfirmPw;
    @ViewById
    EditText etName;
    @ViewById
    TextView tvSex;
    @ViewById
    TextView tvBirthday;
    @ViewById
    EditText etEMail;
    @ViewById
    EditText etHeight;
    @ViewById
    EditText etWeight;


    User mRegisterUser;

    @AfterViews
    void initViews() {
        initToolBar();
        mRegisterUser = new User();
    }

    private void initToolBar() {
        mToolBarTitle.setText(R.string.user_register);
//        mToolbarView.setBackgroundColor(getResources().getColor(R.color.action_bar_bg));
        mToolbarView.setNavigationIcon(R.drawable.ic_back);
        mToolbarView.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    @Click(R.id.llSex)
    void chooseSex() {
        new ChooseSexDialog(this, new ChooseSexDialog.OnSelectSexListener() {
            @Override
            public void onSelect(int sex) {
                switch (sex) {
                    case User.FEMALE:
                        tvSex.setText(R.string.female);
                        break;
                    case User.MALE:
                        tvSex.setText(R.string.male);
                        break;
                }
                mRegisterUser.setSex("" + sex);
            }
        }).show();
    }

    @Click(R.id.llBirthday)
    void chooseBirthday() {
        GregorianCalendar calendar = new GregorianCalendar();
        final int currYear = calendar.get(Calendar.YEAR);
        final int currMonth = calendar.get(Calendar.MONTH);
        final int currDay = calendar.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                if (year > currYear) {
                    return;
                } else if (year == currYear) {
                    if (monthOfYear > currMonth) {
                        return;
                    } else if (monthOfYear == currMonth) {
                        if (dayOfMonth > currDay) {
                            return;
                        }
                    }
                }
                String birthday = year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日";
                mRegisterUser.setBirthday(birthday);
                tvBirthday.setText(birthday);
            }
        }, currYear, currMonth, currDay);
        datePickerDialog.show();
    }

    @Click(R.id.btnNestStep)
    void nextStep() {
        if (TextUtils.isEmpty(etAccount.getText())) {
            MToast.show(R.string.please_enter_account);
            return;
        }
        if (!CheckUtil.checkEMail(etEMail.getText().toString())) {
            return;
        }
        if (!CheckUtil.checkPW(etPw.getText().toString())) {
            return;
        }
        if (!CheckUtil.checkPW(etConfirmPw.getText().toString())) {
            return;
        }
        if (!etPw.getText().toString().equals(etConfirmPw.getText().toString())) {
            MToast.show(R.string.password_do_not_match);
            return;
        }

        mRegisterUser.setLoginID(etAccount.getText().toString());
        mRegisterUser.setPassword(etPw.getText().toString());
        mRegisterUser.seteMail(etEMail.getText().toString());
        mRegisterUser.setUserName(etName.getText().toString());
        mRegisterUser.setHeight(TextUtils.isEmpty(etHeight.getText().toString()) ? "1" : etHeight.getText().toString());
        mRegisterUser.setWeight(TextUtils.isEmpty(etWeight.getText().toString()) ? "1" : etWeight.getText().toString());


        Intent intent = new Intent(this, BindingPhoneActivity_.class);
        intent.putExtra(BindingPhoneActivity.REGISTER_USER, Parcels.wrap(mRegisterUser));
        startActivityForResult(intent, REQUEST_V_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_V_CODE:
                if (resultCode == RESULT_OK) {
                    finish();
                }
                break;
        }
    }
}
